'use strict';
angular.module('User').controller('LoginController', ['$scope', '$cookies', '$state', '$timeout', 'usbPort', 'User', 'Authentication', 'BaseFunction', 
    function ($scope, $cookies, $state, $timeout, usbPort, User, Authentication, BaseFunction) {
        $scope.data = {
            usercode: '',
            password: '',
            rememberme: false,
            activeIndex: 1
        };

        $scope.clickTab = function (index) {
            $scope.data.activeIndex = index;
            if (index === 2) {
                usbPort.start();
            } else {
                usbPort.stop();
            }
        };
        $scope.signin = function () {
            User.signin($scope.data.usercode, $scope.data.password, $scope.data.rememberme).then(function () {
                $state.go('index.home');
            });
        };

        $scope.$on('usbPort.input', function (e, barcode) {
            User.signinScan(barcode).then(function () {
                $state.go('index.home');
            }, function (err) {
                BaseFunction.tip(err.message || '未知的错误');
            });
        });
        $scope.$on('$destroy', function () {
            usbPort.stop();
        });

        angular.element(document).ready(function(){ 
            $timeout(function() {
                angular.element('#promCarousel').carousel({ interval: 4000 });
            });
        });
    }
]);
